//
//  GoogleCertyViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/6.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class GoogleCertyViewModel: BaseViewModel {
    open var model:SecurityModel? //传递的model
    open var googleKey:String = ""
    
    var codeText = "" //验证码
    var googleCode = "" //谷歌验证码
    
    lazy var codeRequest:MobileCodeRequest = {
        let api = MobileCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var bindRequest:GoogleCertyRequest = {
        let api = GoogleCertyRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var emailRequest:EmailCodeRequest = {
        let api = EmailCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    func putInformation() {
        self.request(request: bindRequest, success: {[weak self] (result) in
            CYMBShow.showMessage(kText("谷歌验证成功"))
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
            for popView in ((self?.controller?.navigationController as! WrapNavigationController).wrapViewController?.navigationController?.viewControllers)! {
                let viewController = (popView as? WrapViewController)?.contentViewController
                if (viewController?.isKind(of: SecurityCenterViewController.self))! {
                    self?.controller?.navigationController?.popToViewController(viewController!, animated: true)
                    return
                }
            }
            self?.controller?.navigationController?.popToRootViewController(animated: true)
            
        }) { (request) in
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func sendCode(btn: TimerBtn)  {
        btn.isEnabled = false
        self.request(request: codeRequest, success: { (result) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    func sendEmailCode(btn: TimerBtn) {
        btn.isEnabled = false
        self.request(request: emailRequest, success: { (result) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func archiveCellKind() {
        var array:[CYTableviewItem] = []
        if AppSecurityInfo.default.model.isMobileBind {
            let itemFir = CYTableviewItem(type: .cyItemTimer, name: kText("手机验证码"), placeHolder: kText("请输入手机验证码"))
            array.append(itemFir)
            
        }else {
            let itemFir = CYTableviewItem(type: .cyItemTimer, name: kText("邮箱验证码"), placeHolder: kText("请输入邮箱验证码"))
            array.append(itemFir)
        }
        
        let itemSec = CYTableviewItem(type: .cyItemNumber, name: kText("谷歌验证码"), placeHolder: kText("请输入谷歌验证码"))
        array.append(itemSec)
        cellfactory.iteamArray.append(array)
    }
}

extension GoogleCertyViewModel:RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? MobileCodeRequest {
            pramas.updateValue(4, forKey: "codeType")
            pramas.updateValue(model?.areaCode.ch_length != 0 ? model?.areaCode as Any : "" , forKey: "areaCode")
            pramas.updateValue(model?.mobile.ch_length != 0 ? model?.mobile as Any : "", forKey: "mobile")
            pramas.updateValue(0, forKey: "type")
        }else if let _ = request as? GoogleCertyRequest {
            pramas.updateValue(4, forKey: "codeType")
            pramas.updateValue(googleKey, forKey: "googleKey")
            pramas.updateValue(googleCode, forKey: "googleCode")
            if (model?.isMobileBind)! {
                pramas.updateValue(model?.areaCode.ch_length != 0 ? model?.areaCode as Any : "" , forKey: "areaCode")
                pramas.updateValue(model?.mobile.ch_length != 0 ? model?.mobile as Any : "", forKey: "mobile")
                pramas.updateValue(codeText, forKey: "mobileCode")
                pramas.updateValue(0, forKey: "type")
            }else {
                pramas.updateValue(1, forKey: "type")
                pramas.updateValue(model?.email.ch_length != 0 ? model?.email as Any : "", forKey: "email")
                 pramas.updateValue(codeText, forKey: "emailCode")
            }
        }else if let _ = request as? EmailCodeRequest {
            pramas.updateValue(4, forKey: "codeType")
            pramas.updateValue(model?.email.ch_length != 0 ? model?.email as Any : "", forKey: "email")
            pramas.updateValue(1, forKey: "type")
        }
        
        return pramas
    }
    
    
}
